
#include "OgreFrameListener.h"

OgreFrameListener::OgreFrameListener(
	Ogre::RenderWindow* win,
	Ogre::Camera* cam,
	Ogre::Viewport* viewport,
	Ogre::SceneNode* node,
	Ogre::Entity* ent
	) : _Cam(cam), _viewport(viewport), _node(node)
{
	_aniStateTop = ent->getAnimationState("RunTop");
	_aniStateTop->setLoop(false);
	_aniStateBottom = ent->getAnimationState("RunBase");
	_aniStateBottom->setLoop(false);

	_MovementSpeed = 50.0f;
	_WalkingSpeed = 50.0f;

	_comp1 = false;
	_comp2 = false;
	_comp3 = false;

	_down1 = false;
	_down2 = false;
	_down3 = false;

	OIS::ParamList parameters;
	unsigned int windowHandle = 0;
	std::ostringstream windowHandleString;

	win->getCustomAttribute("WINDOW", &windowHandle);
	windowHandleString << windowHandle;

	parameters.insert(std::make_pair("WINDOW", windowHandleString.str()));

	_InputManager = OIS::InputManager::createInputSystem(parameters);
	_Keyboard = static_cast<OIS::Keyboard*>(_InputManager->createInputObject(OIS::OISKeyboard, false));
	_Mouse = static_cast<OIS::Mouse*>(_InputManager->createInputObject(OIS::OISMouse, false));
}

OgreFrameListener::~OgreFrameListener() {

	_InputManager->destroyInputObject(_Keyboard);
	_InputManager->destroyInputObject(_Mouse);

	OIS::InputManager::destroyInputSystem(_InputManager);
}


bool OgreFrameListener::frameStarted(const Ogre::FrameEvent &evt) {

	Ogre::Vector3 translate(0, 0, 0);
	Ogre::Vector3 SinbadTranslate(0, 0, 0);

	float _rotation = 0.0f;

	bool walked = false;


	_Keyboard->capture();

	if(_Keyboard->isKeyDown(OIS::KC_ESCAPE)) { return false; }

	if(_Keyboard->isKeyDown(OIS::KC_W)) {
		translate += Ogre::Vector3(0, 0, -1);
	}
	if(_Keyboard->isKeyDown(OIS::KC_S)) {
		translate += Ogre::Vector3(0, 0, 1);
	}
	if(_Keyboard->isKeyDown(OIS::KC_A)) {
		translate += Ogre::Vector3(-1, 0, 0);
	}
	if(_Keyboard->isKeyDown(OIS::KC_D)) {
		translate += Ogre::Vector3(1, 0, 0);
	}
	
	if(_Keyboard->isKeyDown(OIS::KC_1) && !_down1) {
		_down1 = true;
		_comp1 = !_comp1;
		//Ogre::CompositorManager::getSingleton().setCompositorEnabled(_viewport, "Compositor2", _comp1);
	}
	if(_Keyboard->isKeyDown(OIS::KC_2) && !_down2) {
		_down2 = true;
		_comp2 = !_comp2;
		//Ogre::CompositorManager::getSingleton().setCompositorEnabled(_viewport, "Compositor3", _comp2);
	}
	if(_Keyboard->isKeyDown(OIS::KC_3) && !_down3) {
		_down3 = true;
		_comp3 = !_comp3;
		//Ogre::CompositorManager::getSingleton().setCompositorEnabled(_viewport, "Compositor7", _comp3);
	}

	if(!_Keyboard->isKeyDown(OIS::KC_1)) {
		_down1 = false;
	}
	if(!_Keyboard->isKeyDown(OIS::KC_2)) {
		_down2 = false;
	}
	if(!_Keyboard->isKeyDown(OIS::KC_3)) {
		_down3 = false;
	}

	if(_Keyboard->isKeyDown(OIS::KC_UP)) {
		SinbadTranslate += Ogre::Vector3(0, 0, -1);
		_rotation = 3.14f;
		walked = true;
	}
	if(_Keyboard->isKeyDown(OIS::KC_DOWN)) {
		SinbadTranslate += Ogre::Vector3(0, 0, 1);
		_rotation = 0.0f;
		walked = true;
	}
	if(_Keyboard->isKeyDown(OIS::KC_LEFT)) {
		SinbadTranslate += Ogre::Vector3(-1, 0, 0);
		_rotation = -1.57f;
		walked = true;
	}
	if(_Keyboard->isKeyDown(OIS::KC_RIGHT)) {
		SinbadTranslate += Ogre::Vector3(1, 0, 0);
		_rotation = 1.57f;
		walked = true;
	}

	if(walked) {
		
		_aniStateTop->setEnabled(true);
		_aniStateBottom->setEnabled(true);

		if(_aniStateTop->hasEnded()) {

			_aniStateTop->setTimePosition(0.0f);
		}
		if(_aniStateBottom->hasEnded()) {

			_aniStateBottom->setTimePosition(0.0f);
		}

		_node->translate(SinbadTranslate * evt.timeSinceLastFrame * _WalkingSpeed);
		_node->resetOrientation();
		_node->yaw(Ogre::Radian(_rotation));
	}
	else {

		_aniStateTop->setTimePosition(0.0f);
		_aniStateTop->setEnabled(false);
		_aniStateBottom->setTimePosition(0.0f);
		_aniStateBottom->setEnabled(false);
	}

	_aniStateTop->addTime(evt.timeSinceLastFrame);
	_aniStateBottom->addTime(evt.timeSinceLastFrame);
	
	_Cam->moveRelative(translate * evt.timeSinceLastFrame * _MovementSpeed);


	_Mouse->capture();

	float rotX = _Mouse->getMouseState().X.rel * evt.timeSinceLastFrame * -1;
	float rotY = _Mouse->getMouseState().Y.rel * evt.timeSinceLastFrame * -1;

	_Cam->yaw(Ogre::Radian(rotX));
	_Cam->pitch(Ogre::Radian(rotY));

	return true;
}